package model;

import app.App;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class User {

    private int userId;
    private String userName;
    private String password;
    private int jobId;

    public User() {
    }

    public User(int userId, String userName, String password, int jobId) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
        this.jobId = jobId;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getJobId() {
        return jobId;
    }

    public void setJobId(int jobId) {
        this.jobId = jobId;
    }

    public String getJobName() {
        String jobName;
        switch (jobId) {
            case 0:
                jobName = "管理员";
                break;
            case 1:
                jobName = "销售员";
                break;
            case 2:
                jobName = "仓管员";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + jobId);
        }
        return jobName;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", jobId=" + jobId +
                '}';
    }

    //根据用户Id获取用户
    public static User getUser(int userId) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE (`userid` = ?)");
            ps.setObject(1, userId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userid"));
                user.setUserName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setJobId(rs.getInt("jobid"));
                System.out.println(user);
                conn.close();
                return user;
            }
            conn.close();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    //获取用户列表
    public static List<User> getUsers() {
        List<User> userList = new ArrayList<>();

        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM user");
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userid"));
                user.setUserName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setJobId(rs.getInt("jobid"));
                System.out.println(user);
                userList.add(user);
            }
            conn.close();
            return userList;
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    //添加用户
    public static boolean addUser(User user) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement(
                    "INSERT INTO `user` (`userid`, `username`, `password`, `jobid`) VALUES (?, ?, ?, ?);");
            ps.setObject(1,user.getUserId());
            ps.setObject(2, user.getUserName());
            ps.setObject(3, user.getPassword());
            ps.setObject(4,user.getJobId());
            int result = ps.executeUpdate();
            conn.close();//关闭连接
            if (result == 1) {
                System.out.println("添加成功！");
                return true;
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        System.out.println("添加失败！");
        return false;
    }

    //删除用户
    public static boolean delUser(int userId) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement(
                    "DELETE FROM `user` WHERE (`userid` = ?);");
            ps.setObject(1, userId);
            int result = ps.executeUpdate();
            conn.close();//关闭连接
            if (result == 1) {
                System.out.println("删除成功！");
                return true;
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        System.out.println("删除失败！");
        return false;
    }

    //添加用户，修改该用户的Id对应的用户信息
    public static boolean modUser(User newUser) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement(
                    "UPDATE `user` SET `username` = ?, `password` = ?, `jobid` = ?  WHERE (`userid` = ?);");
            ps.setObject(1, newUser.getUserName());
            ps.setObject(2, newUser.getPassword());
            ps.setObject(3, newUser.getJobId());
            ps.setObject(4, newUser.getUserId());
            int result = ps.executeUpdate();
            conn.close();//关闭连接
            if (result == 1) {
                System.out.println("修改成功！");
                return true;
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        System.out.println("修改失败！");
        return false;
    }

    //检查用户是否存在,若正确，返回该用户
    public static User checkUser(String userName, String password) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE username=? AND password=?");
            ps.setObject(1, userName);
            ps.setObject(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int userId = rs.getInt("userid");
                int jobId = rs.getInt("jobid");
                System.out.println("登录成功！");
                System.out.println("用户ID为："+userId);
                System.out.println("账号为："+userName);
                System.out.println("密码为："+password);
                System.out.println("职位号为："+jobId);
                return new User(userId, userName, password ,jobId);
            }
            conn.close();//关闭连接
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        System.out.println("登录失败！");
        return null;
    }

    //获取一个新的用户号
    public static int getNewUserId() {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT MAX(userid) AS max FROM user");
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int max = rs.getInt("max") + 1;
                conn.close();
                return max;
            }
            conn.close();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return 1;
    }
}
